package com.huika.renrench.support.bean;

import java.io.Serializable;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.huika.renrench.control.common.base.BaseApp;
/**
 * 定位数据bean
 * @author fanxing 创建于 Mar 17, 2015
 */
public class LocationBean implements Serializable{
	public static final String LATITUDE = "latitude";//维度
	public static final String LONGTITUDE = "longtitude";//经度
	public static final String PROVINCE = "Province";//省
	public static final String CITY = "City";//市
	public static final String DISTRICT = "District";//区县
	public static final String LOCTYPE = "locType";//定位结果类型
	
	public interface MyLocationListener{
		/**
		 * 无网络情况下反地理编码失败获取不到省市区
		 * 特定情况下根据locType进行辨别定位结果,
		*/
		void onLocation(LocationBean locationBean);
	}
	
	public double latitude, longtitude;
	public String province, city, district;
	public String road, poi;//具体街道和精确位置
	public String focusAddress;//除去省市区外的详细地址，为road和poi的和
	/**
	 * 百度地图返回值：
61 ： GPS定位结果
62 ： 扫描整合定位依据失败。此时定位结果无效。
63 ： 网络异常，没有成功向服务器发起请求。此时定位结果无效。
65 ： 定位缓存的结果。
66 ： 离线定位结果。通过requestOfflineLocaiton调用时对应的返回结果
67 ： 离线定位失败。通过requestOfflineLocaiton调用时对应的返回结果
68 ： 网络连接失败时，查找本地离线定位时对应的返回结果
161： 表示网络定位结果
162~167： 服务端定位失败
502：key参数错误
505：key不存在或者非法
601：key服务被开发者自己禁用
602：key mcode不匹配
501～700：key验证失败
---------------------------------------
高德地图返回值：
0	正常
21	IO 操作异常
22	连接异常
23	连接超时
24	无效的参数
25	空指针异常
26	url 异常
27	未知主机
28	服务器连接失败
29	协议解析错误
30	http 连接失败
31	未知的错误
32	key 鉴权失败
33	获取基站/WiFi信息为空或失败
34	定位失败无法获取城市信息
	 */
	public int locType;
	
	public LocationBean(double latitude, double longtitude, String province, String city, String district,
			int locType) {
		super();
		this.latitude = latitude;
		this.longtitude = longtitude;
		this.province = province;
		this.city = city;
		this.district = district;
		this.locType = locType;
	}
	
	public LocationBean(int locType) {
		this.locType = locType;
	}
	public LocationBean() {
	}

	/**获取上一次应用定位的结果*/
	public static LocationBean getLastLocation(){
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(BaseApp.getInstance());
		String lat = sp.getString(LATITUDE, null);
		String lon = sp.getString(LONGTITUDE, null);
		if(TextUtils.isEmpty(lat) || TextUtils.isEmpty(lon)){
			return null;
		}
		
		String pro = sp.getString(PROVINCE, "");
		String city = sp.getString(CITY, "");
		String dis = sp.getString(DISTRICT, "");
		int locType = sp.getInt(LOCTYPE, 162);
		double latitude = Double.parseDouble(lat);
		double longtitude = Double.parseDouble(lon);
		
		return new LocationBean(latitude, longtitude, pro, city, dis, locType);
	}
	
	/**存储定位结果*/
	public static LocationBean saveCurrentLocation(double latitude, double longtitude, String province, String city, String district,
			int locType){
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(BaseApp.getInstance());
		Editor edit = sp.edit();
		String lat = latitude + "";
		String lon = longtitude + "";
		
		edit.putString(LATITUDE, lat);
		edit.putString(LONGTITUDE, lon);
		edit.putString(PROVINCE, province);
		edit.putString(CITY, city);
		edit.putString(DISTRICT, district);
		edit.putInt(LOCTYPE, locType);
		edit.commit();
		
		return new LocationBean(latitude, longtitude, province, city, district, locType);
	}

	@Override
	public String toString() {
		return toShortAddress() + getDetailAddress();
	}
	
	/**
	 * 获取省市区
	 * @return
	 * @author FAN 创建于Mar 19, 2015
	 */
	public String toShortAddress(){
		return getIfNull(province) + getIfNull(city) + getIfNull(district);
	}
	
	public String getDetailAddress(){
		if(TextUtils.isEmpty(focusAddress)){
			focusAddress = getIfNull(road) + getIfNull(poi);
		}
		return focusAddress;
	}
	
	private String getIfNull(String st){
		return TextUtils.isEmpty(st) ? "" : st;
	}
}
